package com.example.chuntiao.cophook.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.mytools.MyApp;
import com.example.chuntiao.cophook.mytools.PhoneTool;

public class PersonDetailActivity extends AppCompatActivity {

    ImageView phoneToCall1;
    ImageView phoneToCall2;
    ImageView imgPhoto;
    ImageView msg1;
    ImageView msg2;
    TextView txtPhone1;
    TextView txtPhone2;

    TextView txtLocate;
    public static final int REQ_PERMIT_FOR_CALL = 3;
    Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mIntent = getIntent();

        setTitle(mIntent.getStringExtra("uname"));

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final String phoneNum1 = mIntent.getStringExtra("phone1");
        final String phoneNum2 = mIntent.getStringExtra("phone2");
        final String locate = mIntent.getStringExtra("locate");
        int imgVer = mIntent.getIntExtra("img_ver", -1);
        String uid = mIntent.getStringExtra("uid");


        phoneToCall1 = (ImageView) findViewById(R.id.img_phone_call);
        msg1 = (ImageView) findViewById(R.id.img_msg);
        txtPhone1 = (TextView) findViewById(R.id.txt_phone1);
        imgPhoto = (ImageView) findViewById(R.id.img_photo);

        txtPhone1.setText(phoneNum1);
        phoneToCall1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ContextCompat.checkSelfPermission(PersonDetailActivity.this, Manifest.permission.CALL_PHONE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PersonDetailActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQ_PERMIT_FOR_CALL);
                } else {
                    PhoneTool.call(phoneNum1, PersonDetailActivity.this);
                }
            }
        });

        msg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhoneTool.send(phoneNum1, PersonDetailActivity.this);
            }
        });


        txtLocate = (TextView) findViewById(R.id.txt_locate);

        if (imgVer != -1)
            Glide.with(PersonDetailActivity.this).load(MyApp.HOST_URL + "image/" + "u_" + uid)
                    .apply(RequestOptions.signatureOf(new ObjectKey(imgVer))).into(imgPhoto);


        if (locate != null && !locate.equals(""))
            txtLocate.setText(locate);

        if (phoneNum2 != null && !phoneNum2.equals("")) {
            CardView cardView2 = (CardView) findViewById(R.id.cardView2);
            cardView2.setVisibility(View.VISIBLE);

            txtPhone2 = (TextView) findViewById(R.id.txt_phone2);
            phoneToCall2 = (ImageView) findViewById(R.id.img_phone_call2);
            msg2 = (ImageView) findViewById(R.id.img_msg2);
            msg2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhoneTool.send(phoneNum2, PersonDetailActivity.this);
                }
            });


            txtPhone2.setText(phoneNum2);
            phoneToCall2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (ContextCompat.checkSelfPermission(PersonDetailActivity.this, Manifest.permission.CALL_PHONE)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(PersonDetailActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQ_PERMIT_FOR_CALL);
                    } else {
                        PhoneTool.call(phoneNum2, PersonDetailActivity.this);
                    }
                }
            });
        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
